package com.jhgsys.internal.hazard.service.impl;

import com.jhgsys.internal.hazard.entity.HazardModuleConfig;
import com.jhgsys.internal.hazard.mapper.HazardModuleConfigMapper;
import com.jhgsys.internal.hazard.service.IHazardModuleConfigService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author yangjie
 * @date 2024-09-10 17:04:26
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HazardModuleConfigServiceImpl extends ServiceImpl<HazardModuleConfigMapper, HazardModuleConfig> implements IHazardModuleConfigService {

    /**
     * 查询（分页）
     * @author yangjie
     * @date 2024-09-10 17:04:26
     * @param request QueryRequest
     * @param hazardModuleConfig hazardModuleConfig
     * @return IPage<HazardModuleConfig>
    */
    @Override
    public IPage<HazardModuleConfig> findHazardModuleConfigPage(QueryRequest request, HazardModuleConfig hazardModuleConfig) {
        LambdaQueryWrapper<HazardModuleConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardModuleConfig::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<HazardModuleConfig> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author yangjie
     * @date 2024-09-10 17:04:26
     * @param hazardModuleConfig hazardModuleConfig
     * @return List<HazardModuleConfig>
     */
    @Override
    public List<HazardModuleConfig> findHazardModuleConfigList(HazardModuleConfig hazardModuleConfig) {
	    LambdaQueryWrapper<HazardModuleConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardModuleConfig::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author yangjie
    * @date 2024-09-10 17:04:26
    * @param id
    * @return HazardModuleConfig
    */
    @Override
    public HazardModuleConfig findHazardModuleConfigById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author yangjie
     * @date 2024-09-10 17:04:26
     * @param hazardModuleConfig hazardModuleConfig
     */
    @Override
    @Transactional
    public void createHazardModuleConfig(HazardModuleConfig hazardModuleConfig) {
        hazardModuleConfig.setCreatedTime(new Date());
        this.save(hazardModuleConfig);
    }

    /**
     * 修改
     * @author yangjie
     * @date 2024-09-10 17:04:26
     * @param hazardModuleConfig hazardModuleConfig
     */
    @Override
    @Transactional
    public void updateHazardModuleConfig(HazardModuleConfig hazardModuleConfig) {
        hazardModuleConfig.setUpdatedTime(new Date());
        this.saveOrUpdate(hazardModuleConfig);
    }

    /**
     * 逻辑删除
     * @author yangjie
     * @date 2024-09-10 17:04:26
     * @param hazardModuleConfig hazardModuleConfig
     * @param hazardModuleConfigIds hazardModuleConfigIds
     */
    @Override
    @Transactional
    public void deleteHazardModuleConfig(HazardModuleConfig hazardModuleConfig,String hazardModuleConfigIds) {
        List<String> list = Arrays.asList(hazardModuleConfigIds.split(StringPool.COMMA));
        this.baseMapper.update(hazardModuleConfig,new LambdaQueryWrapper<HazardModuleConfig>().in(HazardModuleConfig::getModuleConfigId,list));
	}

    /**
    * 物理删除
    * @author yangjie
    * @date 2024-09-10 17:04:26
    * @param hazardModuleConfigIds hazardModuleConfigIds
    */
    @Override
    @Transactional
    public void physicallyDeleteHazardModuleConfig(String hazardModuleConfigIds) {
        List<String> list = Arrays.asList(hazardModuleConfigIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardModuleConfig> wapper = new LambdaQueryWrapper<>();
        wapper.in(HazardModuleConfig::getModuleConfigId,list);
        this.remove(wapper);
    }

    /**
     * 根据所属公司查询
     * @param tenantId
     * @return
     */
    @Override
    public HazardModuleConfig findHazardModuleConfigByTenantId(String tenantId){
        LambdaQueryWrapper<HazardModuleConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardModuleConfig::getDeletedFlg, 0);
        queryWrapper.eq(HazardModuleConfig::getTenantId, tenantId);
        queryWrapper.last(" LIMIT 1 ");
        return this.baseMapper.selectOne(queryWrapper);
    }
}
